#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>

#include <stdlib.h>
#include <string.h>
#include "callbacks.h"
#include "interface.h"
#include "support.h"

#include <stdlib.h> //pour l'appel de la fonction atoi()
#include <string.h> //pour l'appel de la fonction strcpy
#include "personne.h" //pour le volet métier de l'application

//variables globales

int sexe=1;
int status=0;

int count=0;

void on_button1_clicked(GtkWidget *objet_graphique, gpointer user_data)
{
	Personne P;
	char chemin[] = "personnes.txt";

	GtkWidget *entry1;
	GtkWidget *entry2;
	GtkWidget *entry3;

	GtkWidget *window2;

	entry1 = lookup_widget(objet_graphique, "entry1");
	entry2 = lookup_widget(objet_graphique, "entry2");
	entry3 = lookup_widget(objet_graphique, "entry3");

	//copier la chaine saisie à partir de entry1 vers P.nom
	strcpy(P.nom, gtk_entry_get_text(GTK_ENTRY(entry1)));
	//copier la chaine saisie à partir de entry2 vers P.nom
	strcpy(P.prenom, gtk_entry_get_text(GTK_ENTRY(entry2)));
	//convertir la chaine saisie à partir de entry3 et la copier dans P.age
	P.age = atoi(gtk_entry_get_text(GTK_ENTRY(entry3)));

	if(sexe == 1)
		strcpy(P.sexe,"HOMME");
	else
		strcpy(P.sexe,"FEMME");

	if(!status)
		strcpy(P.status,"MARIEE");
	else
		strcpy(P.status,"CELIBATAIRE");

	enregistrer_personne(chemin, P);
	//creation et affichage de la deuxième fenetre graphique
	count++;
	if(count==1) {
		window2 = create_window2();
		gtk_widget_show(window2);

	}

}

void on_button2_clicked(GtkWidget *objet_graphique, gpointer user_data)
{

	Personne P;
	char chemin[] = "personnes.txt";


	char Buf0[200] = "NOM: ";
	char Buf1[50] = "PRENOM: ";
	char Buf2[50] = "AGE: ";
	char Buf3[50] = "SEXE: ";
	char Buf4[50] = "STATUS: ";


	GtkWidget * label5;

	label5= lookup_widget(objet_graphique, "label5");

	lire_personne(chemin, &P);

	sprintf(Buf0, "%s %s \n\n %s %s \n\n %s %d \n\n %s %s \n\n %s %s", Buf0, P.nom, Buf1,
	        P.prenom, Buf2, P.age,Buf3,P.sexe,Buf4,P.status);

	gtk_label_set_text(GTK_LABEL(label5), Buf0);


}



void on_radiobutton1_pressed (GtkButton *button, gpointer user_data)
{
	sexe=1;
}

void on_radiobutton2_pressed (GtkButton *button, gpointer user_data)
{
	sexe=2;
}

void on_checkbutton1_toggled (GtkToggleButton *togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(togglebutton))) {
		status=1;
	} else {
		status=0;
	}

}

void on_window1_destroy (GtkObject *object, gpointer user_data)
{
	gtk_main_quit();
}
void on_window2_destroy (GtkObject *object, gpointer user_data)
{
	count=0;
}
